package asia.doing.controller.manage;

import asia.doing.common.Result;
import asia.doing.entity.Category;
import asia.doing.service.manage.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "分类管理" ,description = "后台管理系统分类相关接口")
@RestController(value = "manageCategoryController")
@RequestMapping("/v1/manage/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * 根据分类id查询分类信息以及对应的文章
     * @param id
     * @return
     */
    @ApiOperation(value = "id查询分类")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Category category = new Category(id);
        List<Category> categories = categoryService.selectCategory(category);
        return Result.success(categories.get(0));
    }

    @ApiOperation(value = "添加分类")
    @PostMapping
    public Result addArticle(@RequestBody Category category) {
        categoryService.addCategory(category);
        return Result.success();
    }

    @ApiOperation(value = "更新分类")
    @PutMapping
    public Result updateArticle(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }

    @ApiOperation(value = "删除分类")
    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable Integer id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分类分页")
    @GetMapping
    public Result selectPage(Category category,
                             @RequestParam(required = false) Integer pageNum,
                             @RequestParam(required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            List<Category> categories = categoryService.selectCategory(category);
            return Result.success(categories);
        }else{
            PageInfo<Category> page = categoryService.selectPage(category, pageNum, pageSize);
            return Result.success(page);
        }
    }
}
